<?php

// @formatter:off
/**
 * @file ct.activetags.run.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: ct.activetags.run.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/cron/ct.activetags.run.php $
 */
// @formatter:on

class WCActiveTagsCron extends WCCronTool {
	public function run($spacer = "") {
		$ok = true;

		global $wcActiveTagsHolder;
		global $wcOptions;

		$wcActiveTagsHolder->createSpecialTags();

		$groupId = $wcOptions->activated("Group") ? $wcOptions->value("Group") : false;

		$tagName = false;
		if($wcOptions->activated("Tag")) {
			if($wcActiveTagsHolder->isActive($wcOptions->value("Tag"), true)) {
				$tagName = $wcOptions->value("Tag");
			} else {
				echo $spacer.WCColorText::ShellRed("Failed: Tag '{$wcOptions->value("Tag")}' is not an active tag or it doesn't exist.")."\n";
				$ok = false;
			}
		}
		//
		// if there's no problems active tags are executed.
		if($ok) {
			//
			// Walking over each group.
			foreach(WCGroupsHolder::I()->items() as $group) {
				//
				// Ignoring not matching groups when there's a specified
				// one.
				if($groupId && $group->id != $groupId) {
					continue;
				}
				if(is_readable($group->root)) {
					$retrycount = 0;
					$retry = true;
					while($retry && $retrycount < 5) {
						$retry = false;
						$retrycount++;

						echo "{$spacer}Checking group '{$group->name}' ({$group->id}) (try {$retrycount}):\n";
						//
						// Walking over each active tag
						foreach($wcActiveTagsHolder->names(true) as $name) {
							//
							// Ignoring tag names when there's
							// a specified one.
							if($tagName && $name != $tagName) {
								continue;
							}
							//
							// Obtaining active tag hook.
							$tagHook = $wcActiveTagsHolder->hook($name, $group);
							if($tagHook) {
								if($tagHook->hasCronTasks()) {
									echo "{$spacer}\tRunning cron tasks for active tag '{$name}':\n";
									if($tagHook->runCronTasks("{$spacer}\t\t")) {
										$retry = true;
									}
								} else {
									echo "{$spacer}\tActive tag '{$name}' has no cron tasks.\n";
								}
							}
						}
					}
				} elseif($groupId && !is_readable($group->root)) {
					echo $spacer.WCColorText::ShellYellow("Group '{$group->name}' ({$group->id}) is disabled.\n");
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool walks through every active tag and if it has a cron tasks to do, it executes it.\n\n";
		echo "{$spacer}Options:\n";
		echo "{$spacer}\t-g group-id, --group group-id\n";
		echo "{$spacer}\t\tIt only run active tags for a specific group.\n\n";
		echo "{$spacer}\t-t tag-name, --tag tag-name\n";
		echo "{$spacer}\t\tIt only run certain active tag.\n\n";
	}
	protected function loadOptions() {
		if(!$this->_optionsLoaded) {
			global $wcOptions;

			if($wcOptions->addValueOption("Group")) {
				$wcOptions->addOptionTrigger("Group", "g");
				$wcOptions->addOptionTrigger("Group", "group");
			}
			if($wcOptions->addValueOption("Tag")) {
				$wcOptions->addOptionTrigger("Tag", "t");
				$wcOptions->addOptionTrigger("Tag", "tag");
			}

			$wcOptions->check();

			$this->_optionsLoaded = true;
		}
	}
}
